package com.wcbdota.list;

import java.util.ArrayList;

import android.content.Context;
import android.graphics.Bitmap;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;

import com.wcbdota.R;
import com.wcbdota.base.C;
import com.wcbdota.model.Hero;
import com.wcbdota.util.AppUtil;
import com.wcbdota.util.AsyncImageLoader;
import com.wcbdota.util.AsyncImageLoader.ImageCallback;
import com.wcbdota.util.SDUtil;

public class HeroGridAdapterList extends BaseAdapter {

	private LayoutInflater mInflater;
	private Context context;
	private ArrayList<Hero> HeroData;
	private GridView gridView;
	private AsyncImageLoader asyncImageLoader;
	private ViewHolder holder;

	private final class ViewHolder {
		private ImageView im_heroimg;
		private TextView tv_heroname;
	}

	public HeroGridAdapterList(Context context,
			ArrayList<Hero> HeroData, GridView gridView) {
		this.mInflater = LayoutInflater.from(context);
		this.context = context;
		this.HeroData = HeroData;
		this.gridView = gridView;
		asyncImageLoader = new AsyncImageLoader();
	}

	@Override
	public int getCount() {
		return HeroData.size();
	}

	@Override
	public Object getItem(int position) {
		return position;
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		//ViewHolder holder;
		Bitmap cachedImage;
		if (convertView == null) {
			convertView = mInflater.inflate(R.layout.hero_gridview_item,
					parent, false);
			holder = new ViewHolder();
			holder.im_heroimg = (ImageView) convertView
					.findViewById(R.id.im_heroimg);
			holder.tv_heroname = (TextView) convertView
					.findViewById(R.id.tv_heroname);
			convertView.setTag(holder);

		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		
		String imageUrl = C.remoteres.base + C.remoteres.heroes + "/" + HeroData.get(position).getAvatar() + ".png";
		holder.im_heroimg.setTag(imageUrl);
		
		String cacheKey = AppUtil.md5(imageUrl);
		cachedImage = SDUtil.getImage(cacheKey);
		if (cachedImage == null) {
			Log.w("gird", "get cached image null");
			holder.im_heroimg.setImageResource(R.drawable.img_loading);
			asyncImageLoader.setview(holder.im_heroimg);
			cachedImage = asyncImageLoader.loadBitmap(imageUrl, new ImageCallback() {

				@Override
				public void imageLoadSuccess(Bitmap imageBitmap,
						String imageUrl, View view) {
					ImageView imageViewByTag = (ImageView) gridView.findViewWithTag(imageUrl);
					if (imageViewByTag != null) {
						String cacheKey = AppUtil.md5(imageUrl);
						SDUtil.saveImage(imageBitmap, cacheKey);
						imageViewByTag.setImageBitmap(imageBitmap);
					}
				}

				@Override
				public void imageLoadError(String imageUrl, View view) {
					ImageView imageViewByTag = (ImageView) gridView.findViewWithTag(imageUrl);
					if (imageViewByTag != null) {
						imageViewByTag.setImageResource(R.drawable.img_loading_error);
					}
				}
			});
		} else {
			holder.im_heroimg.setImageBitmap(cachedImage);
		}
		
		holder.tv_heroname.setText((String) HeroData.get(position).getHeroName()
				.toString());
		return convertView;
	}
}